<?
class Strings {
	var $tarray	= array();
	
	function __construct($dir, $ext) {
		$num_files = count(glob($dir . '*' .$ext));
		if (!$num_files) {
			//echo 'warning: there are no ' . $ext . ' files in '. $dir;
			return false;
		} else {
			$dirlist = opendir($dir);
			while ($item = readdir($dirlist)) {
				if (!is_dir($item)) {
					$file_ext = substr($item, strrpos($item, '.'));
					if ($file_ext == $ext) {
						$part_name = substr($item, 0, strrpos($item, '.')); 
						$this->tarray[$part_name] = file_get_contents($dir . $item);
					}
				}
			}
			closedir($dirlist);
		}
	}
	
	public function mixArray($inputArray, $stringId) {
		if ((gettype($inputArray) == 'array') && (sizeof($inputArray) > 0)) {
			if ($this->get_array_dimension($inputArray) == 1) {
				$final = $this->replace($inputArray, $this->tarray[$stringId]);
			} else {
				for($x=0; $x<sizeof($inputArray); $x++) {
					$tstring = $this->replace($inputArray[$x], $this->tarray[$stringId]);
					$final .= $tstring;
				}
			}
			//var_dump($final);
			return $final;
		} else return $this->tarray[$stringId];
	}
	
	public function mixStrings($part) {
		preg_match_all('#{([a-zA-Z0-9_]+)}#', $this->get($part), $replaces);
		if (count($replaces[0]) > 0) {
			$i=0;
			while($item = $replaces[0][$i]) {
				$this->set($part, str_replace($item, $this->mixStrings($replaces[1][$i]), $this->get($part)));
				$i++;
			}
		}
		return $this->get($part);
	}
	
	private function replace($inputArray, $string) {
		foreach ($inputArray as $key=>$value) {
			if ($string != null) $string = str_replace('{'.$key.'}', $value, $string);
		}
		return $string;
	}
	
	public function set($stringId, $string) {
		$this->tarray[$stringId] = $string;
	}
	
	public function get($stringId) {
		return $this->tarray[$stringId];
	}
	
	private function get_array_dimension($array) {
		$x = 0;
		if (gettype($array) == 'array') {
			if (!empty($array)) $x++;
			$temp_array = current($array);
			$x += $this->get_array_dimension($temp_array);
		}
		return $x;
	}
}
?>